<template>
  <div>
    <a-card :bordered="false">
      <f-search-form :more="false" @searchHandler="searchHandler">
        <f-search-form-item label="标题" name="title"/>
      </f-search-form>
      <f-table ref="table" size="default" rowKey="contentId" :scroll="scroll" :columns="columns" :multiple="false" :operator="operator" :params="queryParam" :api="api.bbsContent">
        <span slot="coverImage" slot-scope="text, record, index">
          <f-images :value="record.coverImage"></f-images>
        </span>
        <span slot="bbsTitle" slot-scope="text, record, index">
             <f-elipsis :text="record.title"></f-elipsis>
        </span>
        <span slot="setStatus" slot-scope="text, record, index">
          <a-switch v-model="record.contentState==1" checkedChildren="已发布" unCheckedChildren="未发布" @change="(checked) => setStatus(checked,record)"/>
        </span>
        <span slot="recommend" slot-scope="text, record, index">
          <a-switch v-model="record.recommend==1" checkedChildren="已推荐" unCheckedChildren="未推荐" @change="(checked) => setRecommend(checked,record)"/>
        </span>
        <span slot="topOrder" slot-scope="text, record, index">
          <a-input-number :value="record.topOrder" @change="(event) =>onChangeTopOrder(event,record)" placeholder="排序号" :maxLength="10" style="width: 60px"/>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a href="javascript:;" @click="$refs.table.remove(record.contentId)">删除</a>
            <a-divider type="vertical"/>
              <a href="javascript:;" @click="lookComment(record.contentId)">查看评论</a>
          </template>

        </span>
      </f-table>
      <bbs-drawer ref="modal"></bbs-drawer>
      <comment-drawer ref="commentModal" :contentId="nowContentId" :commentType="2" title="帖子评论"></comment-drawer>
    </a-card>
  </div>
</template>

<script>

  import BbsDrawer from './bbs_drawer'
  import CommentDrawer from '../comment/comment_drawer'

  export default {
    name: 'TableList',
    components: {
      BbsDrawer,
      CommentDrawer
    },
    data() {
      let me = this
      return {
        //列表初始化查询参数
        queryParam: {},
        scroll: { x: 1300 },
        nowContentId: '',
        // 表头
        columns: [
          {
            title: '#',
            width: '60',
            align: 'center',
            customRender: (text, record, index) => index + 1
          },
          {
            title: '标题',
            scopedSlots: { customRender: 'bbsTitle' },
            dataIndex: 'title'
          },
          {
            title: '发布人',
            dataIndex: 'nickName'
          },
          {
            title: '发布状态',
            align: 'center',
            dataIndex: 'contentState',
            scopedSlots: { customRender: 'setStatus' }
          },
          {
            title: '推荐状态',
            align: 'center',
            dataIndex: 'recommend',
            scopedSlots: { customRender: 'recommend' }
          },
          {
            title: '图片',
            align: 'center',
            scopedSlots: { customRender: 'coverImage' }
          },
          {
            title: '排序号',
            align: 'center',
            dataIndex: 'topOrder',
            scopedSlots: { customRender: 'topOrder' }
          },
          {
            title: '评论数量',
            align: 'center',
            dataIndex: 'commentNum'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            fixed: 'right',
            scopedSlots: { customRender: 'action' }
          }
        ],
        //操作按钮
        operator: [
          {
            name: '删除',
            icon: 'delete',
            batch: true,
            handler: function(table) {
              table.remove()
            }
          }
        ]
      }
    },
    created() {

    },
    methods: {
      setStatus(check, record) {
        this.http.post(this, {
          url: this.api.bbsContent.update,
          data: {
            ids: record.contentId,
            contentState: Number(check)
          },
          success(me, data) {
            record.contentState = data.contentState
          }
        })
      },
      setRecommend(check, record) {
        this.http.post(this, {
          url: this.api.bbsContent.update,
          data: {
            ids: record.contentId,
            recommend: Number(check)
          },
          success(me, data) {
            record.recommend = data.recommend
          }
        })
      },
      onChangeTopOrder(value, record) {
        if (isNaN(value) == false) {
          this.http.post(this, {
            url: this.api.bbsContent.update,
            data: {
              ids: record.contentId,
              topOrder: value
            },
            success(me, data) {
              record.topOrder = data.topOrder
            }
          })
        }
      },
      lookComment(contentId) {
        this.nowContentId = contentId
        this.$refs.commentModal.handleShow()
      }
    }
  }
</script>
